<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

class CheckLoginAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        //是否时admin登录
        if(Auth::guard(session('guard'))->check()){
            //检查禁用状态
            if(Auth::guard(session('guard'))->user()->getAttribute('is_locked')==1){
                Auth::guard(session('guard'))->logout();
                return redirect(\route('admin.login'))->withErrors("您的账号已被禁用，请联系管理员");
            }
            //判断当前url是否是登陆页
            if(Route::currentRouteName()=='admin.loginShow'){
                return redirect(\route('admin.login.already'));
            }
        }
        return $next($request);
    }
}
